import { exportDB, importDB } from 'dexie-export-import'
import { assetsDB } from '@/indexDB/index'
import { ElMessageBox, ElMessage } from 'element-plus'

export const exportIndexDB = () => {

  exportDB(assetsDB).then((blob) => {
    const url = URL.createObjectURL(blob)
    const aLink = document.createElement('a')
    aLink.href = url
    aLink.setAttribute('download', '数据.json')
    aLink.click()
  })
}

export const importIndexDB = (file: any) => {
  ElMessageBox.confirm('将会覆盖全部已有数据，确认继续？', '警告', {
    type: 'warning'
  }).then(() => {
    importDB(file, {
      clearTablesBeforeImport: true
    }).then(res => {
      console.log(res, '---importIndexDB');
      ElMessage.success('导入成功!')
    }).catch(err => {
      ElMessage.error(`导入失败${err}`)

    })
  })

}